//
//  CelebrityListViewController.m
//  ShangHaiTuJI
//
//  Created by xiaoxiangtuji on 2017/7/25.
//  Copyright © 2017年 xiaoxiangtuji. All rights reserved.
//

#import "CelebrityListViewController.h"
#import "CelebrityListCell.h"
#import "CelebrityHeaderCell.h"
#import "OpusListModel.h"
#import "BigIntellgientModel.h"
#import "MusicViewController.h"
#import "IntorduceAndCatalogViewController.h"
#import "FamousPersonDetailViewController.h"
@interface CelebrityListViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property(nonatomic,strong)UICollectionView *collectionView;
@property(nonatomic,strong)UICollectionViewFlowLayout *flowLayout;
@property(nonatomic,strong)NSMutableArray *dataArray;
@property(nonatomic,strong)NSMutableArray *showreelArray;
@end

@implementation CelebrityListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = ZColorRGB(247,216, 73);
//    [self testYYModelWithFileid:self.opusId];
    [self createCollectionView];
    //根据类型判断
    if ([self.type intValue] == 4) {
         [self getAuthorInformationAccordingFileId:self.opusId];
    }else if ([self.type intValue] == 3){
         [self getlearnSkillCenterListInformationWithOpusid:self.opusId];
    }else{
        [self getCelebritiesBiographiesDataWithType:self.type AndWithShowCount:@"100" andcurrentPage:@"0"];
    }
    
    // Do any additional setup after loading the view.
}

//-(void)createCelebrityListUI{
//    UIImageView *bgImageview = [[UIImageView alloc] init];
//    bgImageview.backgroundColor = [UIColor redColor];
//    [self.view addSubview:bgImageview];
//    [bgImageview mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.width.mas_equalTo(WIDTH);
//        make.top.equalTo(self.view.mas_top);
//        make.height.mas_equalTo(scaleTopAndBottom(300));
//        make.left.equalTo(self.view.mas_left);
//    }];
//    UILabel *nameLabel = [[UILabel alloc] init];
//    nameLabel.backgroundColor = [UIColor yellowColor];
//    [self.view addSubview:nameLabel];
//    [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(bgImageview.mas_bottom).offset(scaleTopAndBottom(30));
//        make.left.equalTo(self.view.mas_left).offset(scaleLeftAndRight(20));
//        make.height.mas_equalTo(scaleTopAndBottom(30));
//        make.width.mas_equalTo(scaleLeftAndRight(110));
//    }];
//    UILabel *titleLabel = [[UILabel alloc] init];
//    titleLabel.backgroundColor = [UIColor cyanColor];
//    [self.view addSubview:titleLabel];
//    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(bgImageview.mas_bottom).offset(scaleTopAndBottom(40));
//        make.right.equalTo(self.view.mas_right);
//        make.height.mas_equalTo(scaleTopAndBottom(20));
//        make.left.equalTo(nameLabel.mas_right).offset(scaleLeftAndRight(10));
//    }];
//    UIView *lineview = [[UIView alloc] init];
//    lineview.backgroundColor = [UIColor whiteColor];
//    [self.view addSubview:lineview];
//    [lineview mas_makeConstraints:^(MASConstraintMaker *make) {
//     make.left.equalTo(self.view.mas_left).offset(scaleLeftAndRight(20));
//      make.height.mas_equalTo(scaleTopAndBottom(2));
//        make.right.equalTo(self.view.mas_right).offset(scaleLeftAndRight(-20));
//        make.top.equalTo(titleLabel.mas_bottom).offset(scaleTopAndBottom(5));
//    }];
//    UILabel *introduceLabel = [[UILabel alloc] init];
//    introduceLabel.backgroundColor = [UIColor greenColor];
//    introduceLabel.numberOfLines = 0;
//    CGSize maximumLabelSize = CGSizeMake(100, 9999);//labelsize的最大值
//    [self.view addSubview:introduceLabel];
//    [introduceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//      make.left.equalTo(self.view.mas_left).offset(scaleLeftAndRight(20));
//        make.right.equalTo(self.view.mas_right).offset(scaleLeftAndRight(-20));
//        make.top.equalTo(lineview.mas_bottom).offset(scaleTopAndBottom(5));
//        make.height.mas_equalTo(200);
//    }];
//   
//}
-(void)createCollectionView{
    self.flowLayout = [[UICollectionViewFlowLayout alloc] init];
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64, WIDTH, HEIGHT -64) collectionViewLayout:self.flowLayout];
    [self.view addSubview:self.collectionView];
    self.collectionView.backgroundColor = ZColorRGB(247, 216, 73);
//    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(self.view.mas_bottom);
//        make.left.equalTo(self.view.mas_left);
//        make.right.equalTo(self.view.mas_right);
//        make.bottom.equalTo(self.view.mas_bottom).offset(scaleTopAndBottom(-44));
//    }];
    self.collectionView.backgroundColor = ZColorRGB(247, 216, 73);
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
//    UINib *nib = [UINib nibWithNibName:@"CelebrityListCell" bundle:nil];
//    [self.collectionView registerNib:nib forCellWithReuseIdentifier:@"CelebrityListCell"];
//    [self.collectionView registerClass:[CelebrityHeaderCell class] forCellWithReuseIdentifier:@"CelebrityHeaderCell"];
    [self.collectionView registerClass:[CelebrityHeaderCell class] forCellWithReuseIdentifier:@"CelebrityHeaderCell"];
    [self.collectionView registerClass:[CelebrityListCell class] forCellWithReuseIdentifier:@"CelebrityListCell"];
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"listHeader"];
}
#pragma mark collectionView的代理方法
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 2;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (section == 0) {
        return self.dataArray.count;
    }else{
        return self.showreelArray.count;
    }
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        
        CelebrityHeaderCell *item = [collectionView dequeueReusableCellWithReuseIdentifier:@"CelebrityHeaderCell" forIndexPath:indexPath];
        if ([self.type intValue] == 4) {
           item.model = self.dataArray[indexPath.row];
        }else if ([self.type intValue] == 3){
            item.headerModel = self.dataArray[indexPath.row];
        }else{
            
        }
        return item;
    }else{
        CelebrityListCell *item = [collectionView dequeueReusableCellWithReuseIdentifier:@"CelebrityListCell"forIndexPath:indexPath];
        item.model = self.showreelArray[indexPath.row];
        return item;
    }

}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        if ([self.type intValue] == 4) {
            BigIntellgientModel *model = self.dataArray[indexPath.row];
            CGFloat labelHeight = [self getHeightLineWithString:[NSString stringWithFormat:@"%@",model.auhtorDescrp] withWidth:scaleLeftAndRight(335) withFont:[UIFont systemFontOfSize:17.0f]];
            NSLog(@"我是他么的高度%f",labelHeight);
            return CGSizeMake(WIDTH,scaleTopAndBottom(410) + labelHeight);
        }else if([self.type intValue] == 3){
            OpusListModel *model = self.dataArray[indexPath.row];
            CGFloat labelHeight = [self getHeightLineWithString:[NSString stringWithFormat:@"%@",model.description] withWidth:scaleLeftAndRight(335) withFont:[UIFont systemFontOfSize:17.0f]];
            NSLog(@"我是他么的高度%f",labelHeight);
            return CGSizeMake(WIDTH,scaleTopAndBottom(410) + labelHeight);
        }else{
           return CGSizeMake(WIDTH,scaleTopAndBottom(410) + 20);
        }

    }else{
        //(WIDTH - 10)/2
      return CGSizeMake(scaleLeftAndRight(180.4999f), scaleTopAndBottom(190));
    }
}
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 0;
}
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    if (section == 0) {
        return 0;
    }else{
        return 0.0000f;
    }
}
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    if (section == 0) {
        return UIEdgeInsetsMake(0, 0, 0, 0);
    }else{
        return UIEdgeInsetsMake(0,scaleLeftAndRight(7), 0, scaleLeftAndRight(7));
    }
}
//collectionview的选中状态
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return;
    }
    if ([self.type intValue] == 4) {
        OpusListModel *model = self.showreelArray[indexPath.row];
        MusicViewController *musicVc = [[MusicViewController alloc] init];
        musicVc.fileId = model.opusId;
        musicVc.picPath = model.imagepath;
        musicVc.type = @"4";
        musicVc.title = model.name;
        [self.navigationController pushViewController:musicVc animated:YES];
    }else if([self.type intValue] == 3){
       OpusListModel *model = self.showreelArray[indexPath.row];
        IntorduceAndCatalogViewController *celeVc = [[IntorduceAndCatalogViewController alloc] init];
        celeVc.authorId = model.opusId;
        celeVc.type = @"3";
        [self.navigationController pushViewController:celeVc animated:YES];
    }else{
        OpusListModel *model = self.showreelArray[indexPath.row];
        FamousPersonDetailViewController *personVc = [[FamousPersonDetailViewController alloc] init];
        personVc.authotId = model.opusId;
        NSLog(@"我是坐着的id%@",personVc.authotId);
        personVc.type = @"0";
        [self.navigationController pushViewController:personVc animated:YES];
    }

}
#pragma mark - 注册collectionView的头部视图
//collectionview的头部视图的设置
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return nil;
    }else{
        UICollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"listHeader" forIndexPath:indexPath];
        view.backgroundColor = [UIColor whiteColor];
        UIView *view1 = [[UIView alloc] init];
        view1.backgroundColor = ZColorRGB(247, 216, 73);
        [view addSubview:view1];
        [view1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(view.mas_left);
            make.top.equalTo(view.mas_top);
            make.bottom.equalTo(view.mas_bottom);
            make.width.mas_equalTo(scaleLeftAndRight(7));
        }];
        UIView *view2 = [[UIView alloc] init];
        view2.backgroundColor = ZColorRGB(247, 216, 73);
        [view addSubview:view2];
        [view2 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(view1.mas_right);
            make.top.equalTo(view.mas_top).offset(scaleTopAndBottom(10));
            make.bottom.equalTo(view.mas_bottom).offset(scaleTopAndBottom(-10));
            make.width.mas_equalTo(scaleLeftAndRight(2));
        }];
        UIView *view3 = [[UIView alloc] init];
        view3.backgroundColor = ZColorRGB(247, 216, 73);
        [view addSubview:view3];
        [view3 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(view.mas_right);
            make.top.equalTo(view.mas_top);
            make.bottom.equalTo(view.mas_bottom);
            make.width.mas_equalTo(scaleLeftAndRight(7));
        }];
        UILabel *label = [[UILabel alloc] init];
        label.text = @"作品集";
        [view addSubview:label];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(view.mas_top);
            make.bottom.equalTo(view.mas_bottom);
            make.left.equalTo(view2.mas_right).offset(scaleLeftAndRight(5));
            make.right.equalTo(view3.mas_left);
        }];
        return view;
    }
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return CGSizeZero;
    }else{
        return CGSizeMake(WIDTH, scaleTopAndBottom(30));
    }
}
//header数组的懒加载
-(NSMutableArray *)dataArray{
    if (_dataArray == nil) {
        _dataArray = [[NSMutableArray alloc] init];
    }
    return _dataArray;
}
//作品集数组的懒加载
-(NSMutableArray *)showreelArray{
    if (_showreelArray == nil) {
        _showreelArray = [[NSMutableArray alloc] init];
    }
    return _showreelArray;
}
#pragma mark - 根据字符串计算label高度
- (CGFloat)getHeightLineWithString:(NSString *)string withWidth:(CGFloat)width withFont:(UIFont *)font {
    
    //1.1最大允许绘制的文本范围
    CGSize size = CGSizeMake(width, 2000);
    //1.2配置计算时的行截取方法,和contentLabel对应
    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
    [style setLineSpacing:10];
    //1.3配置计算时的字体的大小
    //1.4配置属性字典
    NSDictionary *dic = @{NSFontAttributeName:font, NSParagraphStyleAttributeName:style};
    //2.计算
    //如果想保留多个枚举值,则枚举值中间加按位或|即可,并不是所有的枚举类型都可以按位或,只有枚举值的赋值中有左移运算符时才可以
    CGFloat height = [string boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:dic context:nil].size.height;
    return height;
}
//根据作品获取作者的信息
-(void)getAuthorInformationAccordingFileId:(NSString *)fielId{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    NSDictionary *param = @{@"id":fielId,@"type":self.type};
    NSString *url = ServerIp@"/opusApp/detail";
    [manager POST:url parameters:param progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        BigIntellgientModel *model = [BigIntellgientModel modelWithDictionary:jsonDict];
        [self.dataArray addObject:model];
        [self getAthorFielListInformationWithAuthorid:model.authorId AndWithFileType:self.type];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
    }];
    
}
//音乐列表界面的网络数据的请求
-(void)getAthorFielListInformationWithAuthorid:(NSString *)authorId AndWithFileType:(NSString *)fileType{
    dispatch_queue_t globelQue = dispatch_get_global_queue(0, 0);
    dispatch_async(globelQue, ^{
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        manager.responseSerializer = [AFHTTPResponseSerializer serializer];
        NSDictionary *param = @{@"id":authorId,@"type":fileType};
        NSString *url = ServerIp@"/authorApp/detail";
        [manager POST:url parameters:param progress:^(NSProgress * _Nonnull uploadProgress) {
            
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
            NSDictionary  *authorDic = jsonDict[@"author"];
            NSArray *opussArra = authorDic[@"opuss"];
            for (NSDictionary *dic in opussArra) {
                OpusListModel *listmodel = [OpusListModel modelWithDictionary:dic];
                [self.showreelArray addObject:listmodel];
            }
           dispatch_async(dispatch_get_main_queue(), ^{
               [self.collectionView reloadData];
           });
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            NSLog(@"%@",error);
        }];
    });
}
//学本领中间类型的列表界面的网络数据的请求
-(void)getlearnSkillCenterListInformationWithOpusid:(NSString *)opusId{
    
    dispatch_queue_t globelQue = dispatch_get_global_queue(0, 0);
    dispatch_async(globelQue, ^{
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        manager.responseSerializer = [AFHTTPResponseSerializer serializer];
        NSDictionary *param = @{@"id":opusId,@"type":self.type};
        NSString *url = ServerIp@"/opusCategoryApp/detail";
        [manager POST:url parameters:param progress:^(NSProgress * _Nonnull uploadProgress) {
            
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
            NSDictionary *categoryDic = jsonDict[@"category"];
            OpusListModel *headerModel = [OpusListModel modelWithJSON:categoryDic];
            [self.dataArray addObject:headerModel];
            NSArray *authorArra = jsonDict[@"authors"];
            for (NSDictionary *dic in authorArra) {
                OpusListModel *listmodel = [OpusListModel modelWithDictionary:dic];
                [self.showreelArray addObject:listmodel];
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.collectionView reloadData];
            });
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            NSLog(@"%@",error);
        }];
    });
}
//名人传记的列表的信息数据的网络请求
-(void)getCelebritiesBiographiesDataWithType:(NSString *)type AndWithShowCount:(NSString *)showcount andcurrentPage:(NSString *)currentpage{
    dispatch_queue_t globelQue = dispatch_get_global_queue(0, 0);
    dispatch_async(globelQue, ^{
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        manager.responseSerializer = [AFHTTPResponseSerializer serializer];
        NSDictionary *param = @{@"type":self.type,@"showCount":showcount,@"currentPage":currentpage};
        NSString *url = ServerIp@"/authorApp/searchListByNewOpus";
        [manager POST:url parameters:param progress:^(NSProgress * _Nonnull uploadProgress) {
            
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
            NSArray *authorArra = jsonDict[@"list"];
            for (NSDictionary *dic in authorArra) {
                OpusListModel *listmodel = [OpusListModel modelWithDictionary:dic];
                [self.showreelArray addObject:listmodel];
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.collectionView reloadData];
            });
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            NSLog(@"%@",error);
        }];
    });
}


@end
